<?php
include "include/config.inc.php";
if(!isset($_SESSION['s_activId']))
{
  header('location:index.php');
}

$payment = isset($_REQUEST['payment']) ? $_REQUEST['payment'] : 0;
if(isset($_POST['submitBtn']))
{
  $transDate   = $_POST['transDateYear']."-".$_POST['transDateMonth']."-".$_POST['transDateDay'];
  $voucherType = isset($_POST['voucherType']) && ($_POST['voucherType'] != '') ? $_POST['voucherType'] : '';
  $voucherNo   = isset($_POST['voucherNo']) && ($_POST['voucherNo'] != '') ? $_POST['voucherNo'] : '';
  
  $insertMaster = "INSERT INTO transmaster (voucherType,transDate,voucherNo)
                   VALUES ('".$voucherType."','".$transDate."','".$voucherNo."')";
  $insertMasterRes = mysql_query($insertMaster);
  $lastMasterId = mysql_insert_id();
  if(!$insertMasterRes)
  {
  	echo "Insert Query Master Not Inserted".mysql_error();
  }
  else
  {
  	$loopCount = 0;
  	while($loopCount < count ($_POST['crAccountId']))
  	{
  		$crAccountId = ($_POST['crAccountId'][$loopCount] != '') ? $_POST['crAccountId'][$loopCount] : 0;
  		$crAmount    = ($_POST['crAmount'][$loopCount] != '') ? $_POST['crAmount'][$loopCount] : 0;
  		if($_POST['crAmount'][$loopCount] != '' && $_POST['crAmount'][$loopCount] > 0)
  		{
  			$insertCrDetail = "INSERT INTO transdetail (transMasterId,crAccountId,crAmount)
			                     VALUES (".$lastMasterId.",".$crAccountId.",".$crAmount.")";
			  $insertCrDetailRes = mysql_query($insertCrDetail);
  		}
  		$loopCount++;
  	}
  	$loopCountDr = 0;
  	while($loopCountDr < count ($_POST['drAccountId']))
  	{
  		$drAccountId = ($_POST['drAccountId'][$loopCountDr] != '') ? $_POST['drAccountId'][$loopCountDr] : 0;
  		$drAmount    = ($_POST['drAmount'][$loopCountDr] != '') ? $_POST['drAmount'][$loopCountDr] : 0;
  		if($_POST['drAmount'][$loopCountDr] != '' && $_POST['drAmount'][$loopCountDr] > 0)
  		{
  			$insertDrDetail = "INSERT INTO transdetail (transMasterId,drAccountId,drAmount)
			                     VALUES (".$lastMasterId.",".$drAccountId.",".$drAmount.")";
			  $insertDrDetailRes = mysql_query($insertDrDetail);
  		}
  		$loopCountDr++;
  	}
  	header("Location:transactionDrCr.php");
  }
  
}

//select Account : Start
$selectAccount = "SELECT accountId,accountName
                    FROM account
                   WHERE isDayBook != 1
                ORDER BY accountName";
$selectAccountRes = mysql_query($selectAccount);
$accArray = array();
$a = 0;
while($accRow = mysql_fetch_array($selectAccountRes))
{
  $accArray['accountId'][$a]   = $accRow['accountId'];
  $accArray['accountName'][$a] = $accRow['accountName'];
  $a++;
}
//select Account : End
////Dr CR Array for Item: Start
$paymentValuesArr[0] = "Payment";
$paymentOutputArr[0] = "Payment";
$paymentValuesArr[1] = "Receipt";
$paymentOutputArr[1] = "Receipt";
$paymentValuesArr[2] = "Journal";
$paymentOutputArr[2] = "Journal";
////Dr CR Array for Item: End
include("bottom.php");
$smarty->assign('paymentValuesArr',$paymentValuesArr);
$smarty->assign('paymentOutputArr',$paymentOutputArr);
$smarty->assign('accArray',$accArray);
$smarty->assign('payment',$payment);
$smarty->display('transactionDrCr.tpl');  
?>